package com.mazaiting.emotion.fragment

import android.os.Bundle
import android.text.TextUtils
import android.view.View
import android.widget.EditText
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.mazaiting.emotion.BasePanelFragment
import com.mazaiting.emotion.R
import com.mazaiting.emotion.adapter.HorizontalRecyclerViewAdapter
import com.mazaiting.emotion.adapter.NoHorizontalScrollerVPAdapter
import com.mazaiting.emotion.model.ImageModel
import com.mazaiting.emotion.util.EmotionUtil
import com.mazaiting.emotion.view.NoHorizontalScrollViewPager
import com.mazaiting.log.L
import com.mazaiting.sp.SpUtil
import java.util.ArrayList

/**
 * 表情碎片
 * @param contentView 内容布局, 用户固定键盘的高度
 * @param editText 输入框
 * @param switchView 切换的布局
 */
class EmotionFragment
constructor(contentView: View, editText: EditText, switchView: View) :
    BasePanelFragment(contentView, editText, switchView) {

    companion object {
        /**
         * 当前位置标记
         */
        private const val CURRENT_POSITION_FLAG = "current_position_flag"
        /**
         * Fragment 集合
         */
        private val MAP_FRAGMENT =
            mapOf(EmotionUtil.EMOTION_CLASSIC_TYPE to EmotionTemplateFragment::class.java.toString())
    }

    /**
     * 碎片列表
     */
    private val mFragments: MutableList<Fragment> = mutableListOf()
    /**
     * 轮播图
     */
    private var mViewPager: NoHorizontalScrollViewPager? = null
    /**
     * 可复用列表
     */
    private var mRecyclerView: RecyclerView? = null
    /**
     * 当前选中位置
     */
    private var mCurrentPosition: Int = 0
    /**
     * 可复用列表适配器
     */
    private var mAdapter: BaseQuickAdapter<ImageModel, BaseViewHolder>? = null

    override fun getLayoutId(): Int = R.layout.fragment_emotion

    override fun bindView(view: View) {
        super.bindView(view)
        initView(view)
        initData()
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        // 初始化--如果不写则采用默认文件名
        SpUtil.instance.initialize(context!!, "emotion")
    }

    /**
     * 初始化视图
     * @param view 视图
     */
    private fun initView(view: View) {
        // 获取轮播图组件
        mViewPager = view.findViewById(R.id.emotion_nh_svp)
        // 获取可复用列表
        mRecyclerView = view.findViewById(R.id.emotion_rv_horizontal)
        // 布局管理器
        val manager = LinearLayoutManager(context)
        // 设置横向
        manager.orientation = LinearLayoutManager.HORIZONTAL
        // 设置
        mRecyclerView?.layoutManager = manager
    }

    /**
     * 初始化数据
     */
    private fun initData() {
        // 初始化轮播图数据
        for (i in 0..6) {
            mFragments.add(EmotionTemplateFragment.newInstance())
        }
        // 横向轮播图适配器
        val vpAdapter = NoHorizontalScrollerVPAdapter(fragmentManager, mFragments)
        mViewPager?.adapter = vpAdapter
        // 初始化底部 Tab
        val list = ArrayList<ImageModel>()
        // 迭代添加数据
        for (i in mFragments.indices) {
            if (i == 0) {
                list.add(
                    ImageModel(
                        "经典笑脸",
                        resources.getDrawable(R.drawable.ic_emotion, null),
                        true
                    )
                )
            } else {
                list.add(
                    ImageModel(
                        "其他笑脸$i",
                        resources.getDrawable(R.drawable.ic_plus, null),
                        false
                    )
                )
            }
        }
        // 设置默认选中
        mCurrentPosition = 0
        // 存储
        SpUtil.instance.put(activity!!, CURRENT_POSITION_FLAG, mCurrentPosition)
        // 使RecyclerView保持固定的大小,这样会提高RecyclerView的性能
        mRecyclerView?.setHasFixedSize(true)
        // 设置布局填充器
        mRecyclerView?.layoutManager = GridLayoutManager(activity, 1, GridLayoutManager.HORIZONTAL, false)
        // 创建适配器
        mAdapter = HorizontalRecyclerViewAdapter()
        // 设置适配器
        mRecyclerView?.adapter = mAdapter
        // 设置数据
        mAdapter?.setNewData(list)
        // 设置条目点击事件
        mAdapter?.setOnItemClickListener { adapter, _, position ->
            // 获取先前被点击tab
            val oldPosition = SpUtil.instance.getInt(activity!!, CURRENT_POSITION_FLAG)
            // 修改背景颜色的标记
            (adapter.getItem(oldPosition) as ImageModel).isSelected = false
            // 记录当前被选中tab下标
            mCurrentPosition = position
            // 设置选择条目
            (adapter.getItem(mCurrentPosition) as ImageModel).isSelected = true
            // 保存
            SpUtil.instance.put(activity!!, CURRENT_POSITION_FLAG, mCurrentPosition)
            //通知更新，这里我们选择性更新就行了
            adapter.notifyItemChanged(oldPosition)
            adapter.notifyItemChanged(mCurrentPosition)
            //viewpager界面切换
            mViewPager?.setCurrentItem(position, false)
        }
    }
}
